<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px; width: 100%"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDep('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 树形 -->
        <el-tree :data="data" :props="defaultProps">
          <!-- eslint-disable-next-line vue/no-template-shadow -->
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">{{ data.name }}</el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDep(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDep(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDep(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 添加部门 -->
      <el-dialog :title="isEdit?'编辑部门':'添加部门'" :visible.sync="dialogFormVisible">
        <depAddDia v-if="dialogFormVisible" ref="depAddDia1" :row="row" :is-edit="isEdit" :pid="pid" :dialog-form-visible.sync="dialogFormVisible" @success="success" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { transArray2 } from '@/utils'
import depAddDia from './depAddDia.vue'
export default {
  components: {
    depAddDia
  },
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogFormVisible: false,
      pid: '',
      isEdit: false,
      row: {}
    }
  },
  created() {
    this.loadDep()
  },
  methods: {
    // 获取部门列表数据
    async loadDep() {
      //  获取组织架构数据列表
      const res = await getDepartments()
      console.log(res, 88)
      if (res.code === 10000) {
        // this.data = res.data.depts
        // 将数据转换为树状数据 - 第三方包
        // this.data = transArray(res.data.depts)

        // 利用递归
        this.data = transArray2(res.data.depts)
        console.log(this.data, 7777)
      }
    },
    // 添加部门
    addDep(pid) {
      /*   2-显示对话框 */
      this.dialogFormVisible = true
      //  获取pid
      this.pid = pid

      // 区分 添加部门 和 编辑部门
      this.isEdit = false
    },
    // 子传父 重新渲染 + 重置表单
    success() {
      // 添加部门成功后执行
      this.dialogFormVisible = false
      this.$refs.depAddDia1.reset()
      this.loadDep()
    },
    // 删除部门
    async delDep(id) {
      const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      // console.log(res)

      if (res === 'confirm') {
        const result = await delDepartment(id)
        // console.log(result)

        if (result.code === 10000) {
          // 成功后=====刷新页面
          this.loadDep()
        }
      }
    },
    // 编辑部门
    editDep(row) {
      // 区分 添加部门 和 编辑部门
      this.isEdit = true

      // 打开对话框
      this.dialogFormVisible = true

      // 接收数据
      this.row = row
    }
  }
}
</script>
<style lang='scss'  scoped>
.departments-container {
 padding: 20px;

 .el-card{
   padding: 30px 120px;

  .el-row {
      width: 100%;
    }
 }
}
</style>
